﻿using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YetAnotherChat.EntityFrameworkDataAccess.RepositoryImp;
using YetAnotherChat.UserManagement.Interfaces;

namespace YetAnotherChat.UnitTest.DependencyInjection
{
    /// <summary>
    /// See <![CDATA[ http://msdn.microsoft.com/en-us/library/ff647854.aspx, http://msdn.microsoft.com/en-us/library/ff648211.aspx#ks_existingobject]]> for more detailed use
    /// </summary>
    [TestClass]
    public class DependencyInjectionTest
    {
        [TestMethod]
        public void TestSimpleInjection()
        {
            //var container = new UnityContainer();
            //container.RegisterType<IChatUserRepository, FakeUserRepository>();

            //Assert.IsTrue(container.IsRegistered<IChatUserRepository>());
            //var repository = container.Resolve<IChatUserRepository>();
            //var backendUser = repository.CreateBackendUser(new Guid(), "Test Backend user", "test@yetanotherchat.chatanotheryet");
            //Assert.IsTrue(backendUser != null && backendUser.DisplayName == "Test Backend user", "Backend user is created:" + (backendUser == null).ToString());
        }

        /// <summary>
        /// This comes in handy when we have something "single instance"
        /// </summary>
        [TestMethod]
        public void TestInjectionWithSingleton()
        {
            //var container = new UnityContainer();
            //container.RegisterType<IChatUserRepository, FakeUserRepository>();
            //// each time we call the resolve method, a random guid signature will be returned
            //var randomGuy = (container.Resolve<IChatUserRepository>() as FakeUserRepository).SingletonSignature;
            //Assert.IsTrue(randomGuy != (container.Resolve<IChatUserRepository>() as FakeUserRepository).SingletonSignature);

            //var fakeUserRepo = new FakeUserRepository() { SingletonSignature = "THIS IS SINGLETON!!!" };
            //container.RegisterInstance<IChatUserRepository>(fakeUserRepo);

            //// after this time, every time we try to get an instance of IChatUserRepository, the signature is still the same
            //Assert.IsTrue("THIS IS SINGLETON!!!" == (container.Resolve<IChatUserRepository>() as FakeUserRepository).SingletonSignature);
            //Assert.IsTrue("THIS IS SINGLETON!!!" == (container.Resolve<IChatUserRepository>() as FakeUserRepository).SingletonSignature);

            //// or we register it with a specifict name, with this, difference instances will be kept
            //fakeUserRepo = new FakeUserRepository() { SingletonSignature = "Another one" };
            //container.RegisterInstance<IChatUserRepository>("anotherOne", fakeUserRepo);
            //Assert.IsTrue("Another one" == (container.Resolve<IChatUserRepository>("anotherOne") as FakeUserRepository).SingletonSignature);
            //Assert.IsTrue("THIS IS SINGLETON!!!" == (container.Resolve<IChatUserRepository>() as FakeUserRepository).SingletonSignature);
        }
    }
}
